package com.cheetah.start.module.monitor.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cheetah.start.common.utils.MyExceptionUtil;
import com.cheetah.start.module.system.domain.MailDo;
import com.cheetah.start.module.system.service.impl.MailService;
import com.cheetah.start.module.system.domain.SysSet;
import com.cheetah.start.module.system.service.SysSetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 25175
 * @date 2022/4/2 13:14
 */
@Slf4j
@Service
public class SendMsgService {
    @Autowired
    private SysSetService setService;
    @Autowired
    private MailService mailService;

    public void noticeSystem(String content) {
        SysSet sysSet = setService.getByKey("email", false);
        String email = sysSet.getSetValue();
        MailDo mailDo = new MailDo(email, "服务器信息", content);
        mailService.sendMail(mailDo);
    }

    public void noticeException(String content) {
        SysSet sysSet = setService.getByKey("email", false);
        String email = sysSet.getSetValue();
        if (StrUtil.isNotBlank(email)) {
            MailDo mailDo = new MailDo(email, "异常信息", content);
            mailService.sendMail(mailDo);
        } else {
            log.warn("email 为空:[{}]", email);
        }

    }

    public void noticeException(Throwable throwable, HttpServletRequest request) {
        String exceptionMsg = MyExceptionUtil.getExceptionMsg(throwable, request);
        noticeException(exceptionMsg);
    }


}
